package com.blb.bio;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.time.LocalDateTime;

/**
 * BIO客户端
 */
public class BIOClient {

    public void sendMessage(String ip,int port,String message){
        //创建连接服务器的socket
        try(Socket socket = new Socket(ip,port);
            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream in = new ObjectInputStream(socket.getInputStream())){
            //通过输出流发送消息给服务器
            out.writeUTF(message);
            out.flush();
            //读取服务器返回的消息
            String msg = in.readUTF();
            System.out.println("服务器说：" + msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new BIOClient().sendMessage("192.168.114.22",7777,"Hello今天是" + LocalDateTime.now());
    }
}
